/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.TreeSet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modele.Dep;
import modele.Med;
import modele.Pays;
import modele.Spe;

/**
 *
 * @author mbamba
 */
public class Control extends HttpServlet {

    private Pays p;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        p = new Pays();
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String page = "";
        String action = request.getParameter("action");
        String choix = request.getParameter("choix");
        String nom=request.getParameter("nom");
        if (action == null) {
            page = "accueil.jsp";
        } else if (action.equals("parDep")) {
            if (choix != null) {
                Collection<Med> m = p.getLeDep(choix).getLesMeds();
                request.setAttribute("listemed", m);
                request.setAttribute("num", choix);
                page = "ListeMedecin.jsp";
            } else {
                Collection<Dep> l = p.getLesDeps();
                request.setAttribute("listedep", l);
                page = "ListeDepartement.jsp";
            }
        }else if (action.equals("parNom")) {
            if(nom==null){
                page = "ListeMedecin2.jsp";
            }else{
                Collection<Med> m = new TreeSet<Med>();
                for (Dep d : p.getLesDeps()) {
                    m.addAll(d.getLesMedes(nom));
                }
                
                request.setAttribute("listemed", m);
                
                page = "ListeMedecin2.jsp";
            }
        }else if (action.equals("parSpe")) {
            if (choix != null) {
                Collection<Med> m = p.getLaSpe(choix).getLesMeds();
                request.setAttribute("listemed", m);
                request.setAttribute("spe", choix);
                page = "ListeMedecin1.jsp";
            } else {
                Collection<Spe> s = p.getLesSpes();
                request.setAttribute("listespe", s);
                page = "ListeSpecialite.jsp";
            }
        }
        request.getRequestDispatcher(page).forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
